﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookStore.Data
{
    public class manufacturerController
    {
        static SqlConnection con = sqlDataProvider.getConnect();
        static DataTable dt;
        #region getbyID
        public static manufacturerInfo getbyID(int manufacturerID)
        {
            manufacturerInfo objManu = new manufacturerInfo();
            using (SqlCommand cmd = new SqlCommand("getIDManufacturer", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@manufacturerID", manufacturerID));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objManu.manufacturerID = dr.GetInt32(0);
                        objManu.manufacturerName = dr.GetString(1);
                        objManu.address = dr.GetString(2);
                        objManu.email = dr.GetString(3);
                        objManu.fax = dr.GetString(4);
                        objManu.description = dr.GetString(5);
                    }
                }
                dr.Close();
            }
            return objManu;
        }

        #endregion

        #region getbyAll
        public static DataTable ListManufacturer()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("listManufacturer", con))
            {
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                dt = new DataTable();
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region insert
        public static bool insertManufacturer(manufacturerInfo objManufacturer)
        {
            using (SqlCommand cmd = new SqlCommand("insertManufacturer", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@manufacturerName", objManufacturer.manufacturerName));
                cmd.Parameters.Add(new SqlParameter("@address", objManufacturer.address));
                cmd.Parameters.Add(new SqlParameter("@email", objManufacturer.email));
                cmd.Parameters.Add(new SqlParameter("@fax", objManufacturer.fax));
                cmd.Parameters.Add(new SqlParameter("@description", objManufacturer.description));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region update
        public static bool updateManufacturer(manufacturerInfo objManufacturer)
        {
            using (SqlCommand cmd = new SqlCommand("updateManufacturer", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@manufacturerID", objManufacturer.manufacturerID));
                cmd.Parameters.Add(new SqlParameter("@manufacturerName", objManufacturer.manufacturerName));
                cmd.Parameters.Add(new SqlParameter("@address", objManufacturer.address));
                cmd.Parameters.Add(new SqlParameter("@email", objManufacturer.email));
                cmd.Parameters.Add(new SqlParameter("@fax", objManufacturer.fax));
                cmd.Parameters.Add(new SqlParameter("@description", objManufacturer.description));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region delete
        public static bool deleteManufacturer(int manufacturerID)
        {
            using (SqlCommand cmd = new SqlCommand("deleteManufacturer", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@manufacturerID", manufacturerID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region checkManu
        public static bool checkManu(int manuID)
        {
            SqlCommand cmd = new SqlCommand("checkManu", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@manufacturerID", manuID));
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                return true;
            }
            dr.Close();
            return false;
        }
        #endregion
    }
}
